package com.xqx.service.impl;

import com.xqx.dao.UserDao;
import com.xqx.domain.Role;
import com.xqx.domain.User;
import com.xqx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author：xingquanxiang createTime：2019/10/5 13:29
 * description:
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    /**
     * 查询所有用户
     *
     * @return
     */
    @Override
    public List<User> findAll(){
        List<User> userList = null;
        try {
            userList = userDao.findAll();
        } catch (Exception e) {
            System.out.println("未查询到数据");
        }
        if (userList!=null){
            for (int i = 0; i < userList.size(); i++) {
                User user = userList.get(i);
                try {
                    user.setRoles(userDao.findRoleByUserId(user.getId()));
                } catch (Exception e) {
                    System.out.println("id为"+user.getId()+"用户没有角色");
                    e.printStackTrace();
                }
            }
        }
        return userList;
    }

    /**
     * 根据用户id删除用户信息
     *
     * @param id
     * @return
     */
    @Override
    public boolean del(int id) {
        if (userDao.del(id)==1){
            return true;
        }
        return false;
    }
}
